//
//  ZZCForgetPasswordOneController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCForgetPasswordOneController.h"
#import "ZZCForgetPasswordTwoController.h"

@interface ZZCForgetPasswordOneController ()

@end

@implementation ZZCForgetPasswordOneController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    [self.phoneTextField becomeFirstResponder];
    [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

}

- (IBAction)nextClick {
    ZZCForgetPasswordTwoController *two = [ZZCForgetPasswordTwoController new];
    two.phone = self.phoneTextField.text;
    [self.navigationController pushViewController:two animated:YES];
}
- (void)textFieldDidChange:(UITextField *)textField{
    UITextRange *selectedRange = textField.markedTextRange;
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        // 没有高亮选择的字
        // 1. 过滤非汉字、字母、数字字符
        self.phoneTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
        // 2. 截取
        if (self.phoneTextField.text.length >= 11) {
            self.phoneTextField.text = [self.phoneTextField.text substringToIndex:11];
        }
    } else {
        // 有高亮选择的字 不做任何操作
    }
}
// 过滤字符串中的非汉字、字母、数字
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
    NSString *filterText = string;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""];
    return result;
}
// return键按下
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    if (textField.text.length < 2) {
        // 提示用户，名称长度至少为2位
        return NO;
    }
    // 设置用户名
    return YES;
}

@end
